<style>
    /* 自定义绿色按钮样式 */
    .layui-btn-green {
        background-color: #4CAF50; /* 绿色 */
        color: #fff; /* 白色文字 */
    }
    .layui-btn-green:hover {
        background-color: #45a049; /* 鼠标悬停时的绿色 */
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
<!--                    <div class="layui-form-item lay-query-content">-->
<!--                        <div class="lay-query-box">-->
<!--                            <div class="query-lt">-->
<!--                                <div class="layui-inline">-->
<!--                                    <label class="layui-form-label w-auto">客户名称：</label>-->
<!--                                    <div class="layui-input-inline">-->
<!--                                        <input name="customerName" class="layui-input" type="text"-->
<!--                                               placeholder="请输入客户名称"/>-->
<!--                                    </div>-->
<!--                                </div>-->
<!--                                <div class="layui-inline">-->
<!--                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" layui-form-keyDownSearch="true" lay-submit>查询</button>-->
<!--                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>-->
<!--                                </div>-->
<!--                            </div>-->
<!--                            <div class="query-rt"></div>-->
<!--                        </div>-->
<!--                    </div>-->
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="contactInfoBtnAdd" class="layui-btn icon-btn">添加
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="contactInfoTable" lay-filter="contactInfoTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="contactInfoTableBar">
    <a class="layui-btn layui-btn-green layui-btn-xs" lay-event="edit">编辑</a>
<!--    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="initPass">初始化密码</a>-->
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="contactInfoForm">
    <form lay-filter="contactInfoForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
            <div class="layui-form-item">
                <label class="layui-form-label">所属客户企业</label>
                <div class="layui-input-block">
                    <input name="customerName" id="customerName" type="text" class="layui-input" lay-verify="required" required disabled
                            ></input>
                    <input type="hidden" name="selCustomerId" id="selCustomerId">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>联系人姓名</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入联系人姓名" type="text" class="layui-input"
                           lay-verify="required" required/>
                </div>
            </div>
<!--        <div class="layui-col-xs6">-->
<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label"><span style="color: red">*</span>账号</label>-->
<!--                <div class="layui-input-block">-->
<!--                    <input id="account" name="account" placeholder="请输入账号" type="text" class="layui-input" lay-verify="account"-->
<!--                           required/>-->
<!--                </div>-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="layui-col-xs6">-->
<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label"><span style="color: red">*</span>密码</label>-->
<!--                <div class="layui-input-block">-->
<!--                    <input id="password" name="password" placeholder="请输入密码" type="password" class="layui-input" lay-verify="password"-->
<!--                           required/>-->
<!--                </div>-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>职务</label>
            <div class="layui-input-block">

                <select name="postId" id="postId" lay-filter="postId" lay-verify="required" lay-search></select>
            </div>
        </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>电话</label>
                <div class="layui-input-block">
                    <input name="telephone" placeholder="请输入电话" type="text" class="layui-input" lay-verify="phone"
                           required/>
                </div>
            </div>
        <div class="layui-form-item">
            <label class="layui-form-label">帐号</label>
            <div class="layui-input-block">
                <input name="account" type="text" placeholder="账号" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input name="password" type="password" placeholder="密码" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>联系人照片</label>
            <div id="btnUpload14" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传文件
            </div>
            <div id="btnUpload14_path" style="margin-left: 260px;line-height: 40px;"></div>
            <input id="btnUpload14_file" name="picture" type="hidden" lay-verify="contactShot" lay-verType="alert"/>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名片</label>
            <div class="layui-input-block">
                <div id="btnUpload15" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传文件
                </div>
                <div id="btnUpload15_path" style="margin-left: 150px;line-height: 40px;"></div>
                <input id="btnUpload15_file" name="visitingCard" type="hidden"/>
            </div>
        </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <select name="sex">
                        <option value="">请选择性别</option>
                        <option value="man">男</option>
                        <option value="woman">女</option>
                        <option value="other">未知</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">所属部门</label>
                <div class="layui-input-block">
                    <input name="department" placeholder="请输入所属部门" type="text" class="layui-input"
                           lay-verify="required" required/>
                </div>
            </div>
        <div class="layui-form-item">
            <label class="layui-form-label">通讯地址</label>
            <div class="layui-input-block">
                <input name="mailAddress" placeholder="请输入通讯地址" type="text" class="layui-input"/>
            </div>
        </div>
            <div class="layui-form-item">
                <label class="layui-form-label">微信号</label>
                <div class="layui-input-block">
                    <input name="wechatNumber" placeholder="请输入微信号" type="text" class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input name="email" placeholder="请输入邮箱" type="text" class="layui-input" lay-verify="email"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <input name="remark" placeholder="请输入备注" type="text" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input name="sort" placeholder="" type="text" class="layui-input"/>
                </div>
            </div>
        <div class="layui-form-item text-right">
<!--            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>-->
            <button class="layui-btn" lay-filter="contactInfoFormSubmit" lay-submit>增加</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script type="text/javascript" src="assets/libs/jquery/jquery.md5.js"></script>

<script>
    layui.use(['layer', 'form', 'table', 'admin', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var config = layui.config;
        var selCustomerId=config.getUser().selCustomerId;

        // 渲染表格
        var insTb = table.render({
            elem: '#contactInfoTable',
            url: config.base + 'enterprise/sell/admin/contactInfo/list?selCustomerId='+selCustomerId,
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号'}
                , {field: 'name', align: 'center', title: '联系人姓名'}
                , {field: 'sexDesc', align: 'center', title: '性别'}
                , {field: 'postName', align: 'center', title: '职务'}
                , {field: 'department', align: 'center', title: '所属部门'}
                , {field: 'email', align: 'center', title: '邮箱', width: 180}
                , {field: 'telephone', align: 'center', title: '电话'}
                , {field: 'customerCode', align: 'center', title: '客户代码'}
                , {field: 'creatorName', align: 'center', title: '创建人'}
                , {field: 'createTime', align: 'center', title: '创建时间', width: 160}
                , {field: 'remark', align: 'center', title: '备注'}
                , {align: 'center', toolbar: '#contactInfoTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);

            }
        });

        // 工具条点击事件
        table.on('tool(contactInfoTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        验证
        form.verify({
            contactShot: function (value, item) {
                if (value === '') {
                    return '请上传联系人照片';
                }
            }
        });

        //监听排序
        table.on('sort(contactInfoTable)', function (obj) {
            table.reload('contactInfoTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#contactInfoBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '820px',
                offset: ['150px', '600px'],
                title: data ? '修改客户联系人信息' : '添加客户联系人信息',
                content: $('#contactInfoForm').html(),
                success: function () {
                    //获取职务
                    admin.req('enterprise/sell/admin/customerContactPost/list', {limit: 999}, (res) => {
                        if (res.code == 200) {
                            loadSelectHtml(res.data, "请选择职务", 'postId', 'name')
                            if (data) {
                                $("#postId").val(data.postId);
                                form.render('select');
                            }
                        }
                    }, 'get');


                    // 获取客户数据
                    admin.req('enterprise/sell/admin/customersInfo/getBasicInfo?userId='+config.getUser().selCustomerId, {}, function (res) {
                        if (res.code == 200) {
                            if (res.data[0]) {
                                $("#customerName").val(res.data[0].customerName);
                                $("#selCustomerId").val(res.data[0].id);
                                // form.val('contactInfoForm', res.data[0]);
                            }

                        } else {
                            layer.msg('获取客户失败', {icon: 2, time: 1000});
                        }
                    }, 'GET');


                    //上传联系人照片
                    uploadImg("btnUpload14", "btnUpload14_path", "btnUpload14_file");

                    if (data && data.picture) {
                        $('#btnUpload14_path').html('<img src="' + data.picture + '" style="height: 35px;" />');
                    }

                    //上传名片
                    uploadImg("btnUpload15", "btnUpload15_path", "btnUpload15_file");
                    if (data && data.visitingCard) {
                        $('#btnUpload15_path').html('<img src="' + data.visitingCard + '" style="height: 35px;" />');
                    }
                    form.val('contactInfoForm', data);

                    // 表单提交事件
                    form.on('submit(contactInfoFormSubmit)', function (d) {

                        layer.load(2);
                        if(!data){
                            d.field.password = md5( d.field.passwor);
                        }
                        admin.req(data ? 'enterprise/sell/admin/contactInfo/update' : 'enterprise/sell/admin/contactInfo/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('contactInfoTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/sell/admin/contactInfo/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#contactInfoBtnExport').click(function () {
            var checkRows = table.checkStatus('contactInfoTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });


        // 上传图片
        function uploadImg(_elem, pathId, inputId) {
            upload.render({
                elem: '#' + _elem
                , url: config.upload_url + 'uploadFile'
                , data: {}
                , accept: 'file'
                , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
                , headers: {Authorization: 'Bearer ' + config.getToken()}
                , done: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg || "上传成功", {icon: 1});
                        let imagUrl = res.data.url;
                        $('#' + pathId).html('<img src="' + imagUrl + '" style="height: 35px;"/>');
                        $('#' + inputId).val(imagUrl);
                    } else if (res.code == 502) {
                        return location.replace('admin/login.html');
                    } else {
                        layer.msg(res.msg || "上传失败", {icon: 2});
                    }
                }
            });
        }

        /**
         * 加载地址HTML
         * @param data 下拉菜单数据
         * @param tips select 默认提示
         * @param htmltarget html要插入的位置
         * @param name  下拉框需要显示值的属性
         */
        function loadSelectHtml(data, tips, htmltarget, name) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                html += `<option value='${data[i].id}'>${data[i][name]}</option>`;
            }
            $("[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

    });
</script>